﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MISAPP.Classify.DataAccess;
using System.Data;
using MISAPP.MISInterface;
using System.IO;

namespace MISAPP.Classify.Service
{
    public class Service_CIB005
    {
        protected DA_CIB005 da = null;
        protected DA_CIB005 DA
        {
            get
            {
                if (da == null)
                {
                    da = new DA_CIB005();
                }
                return da;
            }
        }

        public string[] GetCriteriaArray()
        {
            string[] flds = DA.GetDistinctField();
            System.Collections.ArrayList newFld = new System.Collections.ArrayList();
            foreach (string str in flds)
            {
                newFld.Add(str);
            }
            newFld.Add("YYYYMM1");
            newFld.Add("YYYYMM2");
            return (string[])newFld.ToArray(typeof(string));
        }

        public DataSet GetCriteriaData()
        {
            return DA.GetCriteria();
        }

        public DataTable GetSummaryData(System.Collections.ArrayList p_criteria)
        {
            /*
             * 第一個是 是否由開始當主要月份
             * 第二個是 開始月份+結束月份
             * 第三個是 FTP RATE 要大於多少碼
             * 第四個是 承作利率大於多少碼
             * 第五個是 均額大於多少億
             */
            SelectList sel1, sel2,cur;
            bool isStart = Convert.ToBoolean(p_criteria[0]);
            string ym1, ym2,ftpRate,rate,bal;
            List<iSelectList> criteriaList = p_criteria[1] as List<iSelectList>;
            int iSel1, iSel2;
            iSel1 = 0;
            iSel2 = 1;
            if (criteriaList.Count > 2)//選幣別，會有三個
            {
                iSel1 += 1;
                iSel2 += 1;
                DA.ResetCriteria();
                cur = criteriaList[0] as SelectList;
                DA.AddCriteria(cur.GetField(), DA.GetInString(cur.GetReverse()), cur.GetListArray());
            }
            if (isStart)
            {
                sel1 = criteriaList[iSel1] as SelectList;
                sel2 = criteriaList[iSel2] as SelectList;
            }
            else
            {
                sel1 = criteriaList[iSel2] as SelectList;
                sel2 = criteriaList[iSel1] as SelectList;
            }
            ym1 = sel1.GetListArray().GetValue(0).ToString();
            ym2 = sel2.GetListArray().GetValue(0).ToString();
            ftpRate = p_criteria[2].ToString();
            rate = p_criteria[3].ToString();
            bal = p_criteria[4].ToString();
            return DA.GetSummaryData(ym1, ym2, ftpRate, rate, bal);
        }

        public void test()
        {
            string dll = "DataAccess.dll";
            Type t = null;
            object o = null;

            string configPath = string.Empty;
            if (File.Exists(AppDomain.CurrentDomain.BaseDirectory + dll)) //如果這一層沒有，就再加bin
            {
                configPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, dll);
            }
            else
            {
                configPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory + @"bin\", dll);
            }

            System.Reflection.Assembly instance = System.Reflection.Assembly.LoadFrom(configPath);
            t = instance.GetType("MISAPP.Classify.DataAccess.DA_CIB005");
            o = System.Activator.CreateInstance(t);
            System.Reflection.MethodInfo mi = o.GetType().GetMethod("GetCriteria");
        }
    }
}
